package com.cooba.activitys.setting;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.utils.CommonUtil;
import com.cooba.utils.ServerURL;
import com.cooba.utils.SharedPreferencesUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.text.SimpleDateFormat;

/**
 * Created by Administrator on 2016/7/4.
 */
public class FeedBackActivity extends AppCompatActivity {
    private ImageButton mBack_btn;
    private RadioGroup mRadioGroup;
    private EditText mContent_et;
    private TextView mSubmit_btn;
    private FeedBackActivity mContext;
    private String type;
    private String create_time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        mContext = this;
        initView();
    }

    private void initView() {
        mBack_btn = (ImageButton) findViewById(R.id.feedback_image_back);
        mRadioGroup = (RadioGroup) findViewById(R.id.feedback_rg);
        mContent_et = (EditText) findViewById(R.id.content_et);
        mSubmit_btn = (TextView) findViewById(R.id.submit_feedback_btn);
        mBack_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.error_rb:
                        type = "0";
                        break;
                    case R.id.product_ue_rb:
                        type = "1";
                        break;
                    case R.id.wifi_rb:
                        type = "2";
                        break;
                    case R.id.opinion_rb:
                        type = "3";
                        break;
                    case R.id.other_rb:
                        type = "4";
                        break;
                    default:
                        break;
                }
            }
        });
        mSubmit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                feedBack();
            }
        });
    }

    private void feedBack() {
        if (TextUtils.isEmpty(type)) {
            Toast.makeText(mContext, "请选择反馈类型!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mContent_et.getText().toString().trim())) {
            Toast.makeText(mContext, "请填写反馈内容!", Toast.LENGTH_SHORT).show();
            return;
        }
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        create_time = sDateFormat.format(new java.util.Date());
        params.addBodyParameter("content",mContent_et.getText().toString().trim());
        params.addBodyParameter("create_time",create_time);
        params.addBodyParameter("type",type);
        String user_id= SharedPreferencesUtil.getData(mContext,"login_id","");
        String user_name=SharedPreferencesUtil.getData(mContext,"name","");
        String mac= CommonUtil.getMacAddress(mContext);
        if (!TextUtils.isEmpty(user_id)) {
            params.addBodyParameter("user_id", user_id);
        }
        if (!TextUtils.isEmpty(user_name)) {
            params.addBodyParameter("user_name", user_name);
        }
        params.addBodyParameter("mac",mac);
        httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl+ServerURL.feedBack, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.d("wei",responseInfo.result);
                String status = JSON.parseObject(responseInfo.result).getString("status");
                if (status.equals("1")){
                    Toast.makeText(mContext, "感谢您的反馈!", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(mContext, "反馈失败!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Log.d("wei","链接服务器失败"+msg);
            }
        });
    }
}
